package com.qianfeng.openapi.web.master.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianfeng.openapi.web.master.dto.TableData;
import com.qianfeng.openapi.web.master.dto.TableParam;
import com.qianfeng.openapi.web.master.entity.AppInfo;
import com.qianfeng.openapi.web.master.feign.CacheServiceClient;
import com.qianfeng.openapi.web.master.mapper.AppInfoMapper;
import com.qianfeng.openapi.web.master.service.AppInfoService;
import com.qianfeng.openapi.web.master.utils.SysConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/12/30
 * @Time: 下午9:24
 */
@Service
public class AppInfoServiceImpl implements AppInfoService {

    @Resource
    private AppInfoMapper appInfoMapper;

    @Resource
    private CacheServiceClient cacheServiceClient;


    @Override
    public TableData appInfoTableList(TableParam param) {
        //IPage<T> page, @Param("ew") Wrapper<T> queryWrapper
        IPage iPage = new Page();
        //(nowPage-1)*pageSize = offset
        int current = param.getOffset()/param.getLimit()+1;
        iPage.setCurrent(current);
        iPage.setSize(param.getLimit());
        ((Page) iPage).setSearchCount(true);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(param.getSearch())){
            queryWrapper.like("corp_name",param.getSearch());
        }


        iPage = appInfoMapper.selectPage(iPage,queryWrapper);

        TableData data = new TableData();
        data.setRows(iPage.getRecords());
        data.setTotal(iPage.getTotal());
        return data;
    }

    @Override
    public R batchDelete(List<Integer> id) {
        int i = appInfoMapper.deleteBatchIds(id);
        return i>0?R.ok(""):R.failed("删除失败");    }

    @Override
    public R appInfo(int id) {
        AppInfo appInfo = appInfoMapper.selectById(id);
        return appInfo!=null?R.ok(appInfo):R.failed("");
    }

    @Override
    public R updateAppInfo(AppInfo appInfo) {

        int i = appInfoMapper.updateById(appInfo);
        if(i>0){
            //3.清除缓存中老数据
            cacheServiceClient.delKey(SysConstant.APPKEY+appInfo.getAppKey());

            //4.更新后的新数据同步到缓存中
            Map map = new HashMap();
            map.put("appKey",appInfo.getAppKey());
            map.put("appName",appInfo.getAppName());
            map.put("description",appInfo.getDescription());
            map.put("appSecret",appInfo.getAppSecret());
            map.put("corpName",appInfo.getCorpName());
            map.put("limit",appInfo.getLimit()+"");
            map.put("redirectUrl",appInfo.getRedirectUrl());
            map.put("id",appInfo.getId()+"");

            cacheServiceClient.hmset(SysConstant.APPKEY+appInfo.getAppKey(),map);
        }
        return i>0?R.ok(""):R.failed("修改失败");
    }
}
